package graduate.cslg.com.universityshopping;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import graduate.cslg.com.universityshopping.card.CardActivity;
import graduate.cslg.com.universityshopping.mainPage.GetLostActivity;
import graduate.cslg.com.universityshopping.mainPage.MainFragment;
import graduate.cslg.com.universityshopping.mainPage.TransactionActivity;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private DrawerLayout mDrawerLayout;
    private NavigationView mNavigationView;
    MainFragment mMainFragment;
    private TextView userName;
    String getUserName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
        initFragments();
        initNavigationView();
        SharedPreferences preferences = this.getSharedPreferences("user_info",MODE_PRIVATE);
        getUserName = preferences.getString("user_name", "Tom Wang");
        userName.setText(getUserName);
    }

    private void initViews() {
        mDrawerLayout = findViewById(R.id.drawer_layout);
        mNavigationView = findViewById(R.id.navigation_view);
        View headView = mNavigationView.getHeaderView(0);
        userName = headView.findViewById(R.id.user_name);
    }

    private void initFragments() {
        mMainFragment = new MainFragment();
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.content_main, mMainFragment)
                .show(mMainFragment).commit();
    }

    private void initNavigationView() {
        mNavigationView.setItemIconTintList(null);
        mNavigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_card:
                startActivity(new Intent(this, CardActivity.class));
                mDrawerLayout.closeDrawers();
                break;
            case R.id.item_shop:
                startActivity(new Intent(this, TransactionActivity.class));
                mDrawerLayout.closeDrawers();
                break;
            case R.id.item_lost:
                startActivity(new Intent(this, GetLostActivity.class));
                mDrawerLayout.closeDrawers();
                break;
            case R.id.item_more:
                Toast.makeText(this, "更多功能正在添加中！", Toast.LENGTH_SHORT).show();
                mDrawerLayout.closeDrawers();
                break;
            case R.id.item_feedback:
                startActivity(new Intent(this, FeedBackActivity.class));
                mDrawerLayout.closeDrawers();
                break;
            case R.id.item_about:

                mDrawerLayout.closeDrawers();
                break;
            case R.id.item_setting:

                mDrawerLayout.closeDrawers();
                break;
        }
        return false;
    }

    public void toggleDrawer() {
        if (mDrawerLayout.isDrawerOpen(Gravity.START)) {
            mDrawerLayout.closeDrawer(GravityCompat.START);
        } else {
            mDrawerLayout.openDrawer(GravityCompat.START);
        }
    }
}
